import { _decorator, Button, Component, director, game, instantiate, Node, Sprite, tween, Vec3 } from 'cc';
import ResourceManger from './ResourceManger';
import { AssetType, Clips } from './Enums';
import { AudioManger } from './AudioManger';
import { Tools } from './Tools';
import { Global } from './Global';
import { ResourcePool } from './ResourcePool';
import { LevelAction } from './LevelAction';
import { WxPlatform } from './ad/WxPlatform';
import { LayerAction } from './LayerAction';
import { ElementAction } from './ElementAction';
import { UnitAction } from './UnitAction';
const { ccclass, property } = _decorator;

@ccclass('HomeAction')
export class HomeAction extends Component {

    @property({ type: Node })
    logo: Node = null;
    @property(Sprite)
    load: Sprite = null;
    @property({ type: Node })
    btns_layer: Node = null;

    private isload = true;
    load_num = 0;

    @property({ type: String })
    ad_intersitial_id: string;
    @property({ type: String })
    ad_video_id: string;
    @property({ type: String })
    ad_banner_id: string;



    onLoad() {
        // game.frameRate = 60;
    }

    async test(x: string): Promise<string> {
        console.log(x,">>> test {}");
        return "hell01"
    }


    async start() {

        this.btns_layer.active = false;
        Global.ad_intersitial_id = this.ad_intersitial_id;
        Global.ad_video_id = this.ad_video_id;
        Global.ad_banner_id = this.ad_banner_id;
        WxPlatform.ins.wechat_setting();
        await this.loadRes();
    }

    async loadRes() {
        director.preloadScene("main", () => {
            console.log(">>>main scene preload completed!!");
            this.show_btns();
        });
        await ResourceManger.instance.load_bundle("bundle", 0.2);
        await ResourceManger.instance.load_resource("bundle", AssetType.Prefab, 0.6)
        await ResourceManger.instance.load_resource("bundle", AssetType.Sound, 0.22)
    }

    start_game() {
        AudioManger.instance.playSound(Clips.btn_1);
        director.loadScene("main");
    }

    show_btns() {
        this.load_num++;
        if (this.load_num >= 2) {
            this.load.node.parent.parent.active = false;
            this.btns_layer.active = true;
            let auto_play = true;
            this.btns_layer.children.forEach(btn => {
                if(btn.active){
                    auto_play = false;
                }
            });
            if(auto_play){
                this.start_game();
            }else{
                this.btns_layer.children.forEach(btn => {
                    {
                        auto_play = true;
                    }
                });
            }
        }
    }


    update(deltaTime: number) {
        if (!this.isload) return;
        this.load.fillRange = Global.loading_rate;
        if (Global.loading_rate >= 0.99) {
            this.isload = false;
            this.show_btns();
        }
    }

    check_lvl_data(){
        this._unit_data();
        this._lvl_data();

        let arr = ResourcePool.instance.get_prefabs_for_unit();
        console.log(">>>",arr);
    }
    _unit_data() {
        console.log( "--------------------unit检查开始--------------------");
      let arr =   ResourcePool.instance.get_prefabs_for_unit();
      arr.forEach(level_prebab => {
           
            let lvl_node = instantiate(level_prebab);
            let pin_total = lvl_node.getComponent(UnitAction).get_hole_num();
            let d = pin_total % 3;
            console.log(lvl_node.name, " >>>>>");
            let msg = "没问题"
            if (d == 1) {
                msg = "     数据错误，关卡钉子（空洞）多了1个";
            } else if (d == 2) {
                msg = "     数据错误，关卡钉子（空洞）多了2个";
            }
            console.log( "      钉子总数：", pin_total, "取模:", pin_total % 3, "提示》 ", msg);
      });
        
    }
    _lvl_data() {
        console.log("--------------------等级数据安全性--------------------");
        for (let lvl = 1; lvl <= 150; lvl++) {
            console.log(lvl, " lvl>>>>>");
            let level_prebab = ResourcePool.instance.get_prefab("lvl_" + lvl);
            if (!level_prebab) {
                console.log(lvl, "      没有这个等级");
                break;
            }
            let lvl_node = instantiate(level_prebab);
            let pin_total = lvl_node.getComponent(LevelAction).get_hole_num();
            let d = pin_total % 3;
            let msg = "没问题"
            if (d == 1) {
                msg = "     数据错误，关卡钉子（空洞）多了1个";
            } else if (d == 2) {
                msg = "     数据错误，关卡钉子（空洞）多了2个";
            }
            console.log( "      钉子总数：", pin_total, "取模:", pin_total % 3, "提示》 ", msg);


            {
                lvl_node.children.forEach(unit_node => {
                    // console.log("init_level this.node>>>>>:", this.node);
                    console.log("      钉子总数：",unit_node.name, "       unit 检查");
                    let unit_action = unit_node.getComponent(UnitAction);
                    if (!unit_node.active) {
                        console.log("错误》〉", "  unit node  是隐藏的");
                    }
                    if (!unit_action) {
                        console.log("错误》〉",  " unit action错误");
                    }

                    unit_node.children.forEach(layer_node => {
                        if (!layer_node.active) {
                            console.log("    ",layer_node.name, "layer ","错误》〉", "layer_node>>是隐藏的");
                        }
                         let layer_action = layer_node.getComponent(LayerAction);
                        if (!layer_action) {
                            console.log("    ",layer_node.name, "layer ","错误》〉","layer aciont>> 错误");
                        }
                        {
                            layer_node.children.forEach(ele_node => {
                                if (!ele_node.active) {
                                    console.log("    ",layer_node.name, "layer ","错误》〉", ele_node.name, " 是隐藏的");
                                }
                                if (!ele_node.getComponent(ElementAction)) {
                                    console.log("    ",layer_node.name, "layer ","错误》〉",  ele_node.name, " action 错误");
                                }
                            })
                        }
                    })
                    // console.log("获取钉子数量：",num,layer_node.active);
                });
            }
        }
    }
}

